# -*-coding:utf-8 -*-
import logging
import logzero
from logzero import logger, setup_logger

from third_api.yb import YBApi


def bms_vehicle2gnss_vehicle_info():
    '''获取vin(bms系统的vehicle_id)到新gnss系统vehicle_info的映射
    '''
    yb_api = YBApi()
    vin_vehicle_map = {}
    vehicles_infos = yb_api.get_vehicles()
    for imei, info in vehicles_infos.items():
        vin_vehicle_map[info['vin']] = info
    g_vin_vehicle_map = vin_vehicle_map
    return vin_vehicle_map


def get_logger(name):
    log_format = '%(color)s%(filename)s %(lineno)d - %(asctime)s.%(msecs)03d - %(levelname)s: %(message)s'
    formatter = logzero.LogFormatter(fmt=log_format)
    logzero.setup_default_logger(formatter=formatter)

    # Set a rotating logfile (replaces the previous logfile handler)
    logzero.logfile(f"./logs/bms-kaiyi-migrate.log", formatter=formatter, loglevel=logging.INFO)

    return setup_logger(name=name, logfile=f"./logs/{name}.log", level=logging.INFO)


def type_to_jt808(msg):
    '''映射友邦/开易数据类型到808标准
    '''
    type = int(msg['type'])
    fatigue_type = int(msg["fatigue_type"])
    smoke_phone = int(msg["smoke_phone"])

    if type == 1 or type == 13 or type == 14:
        # 危险变道 # 左急转弯 # 右急转弯
        return 28

    if type == 5 or type == 2 or type == 6 or type == 3:
        # 碰撞预警 # 行人避让提示
        return 29

    if type == 41 or type == 11:
        # AEB紧急制动 # 急加速
        return 0

    if type == 15:
        # 剧烈颠簸或碰撞
        return 30

    if type == 21:
        # 超速报警
        return 1

    if type == 4:
        if fatigue_type == 2 or fatigue_type == 4:
            # 疲劳驾驶预警 # 打哈欠
            return 14

        elif fatigue_type == 3 or fatigue_type == 7 or (smoke_phone == 1 or smoke_phone == 2) or smoke_phone == 3 or type == 30 or type == 12 or type == 27:
            # 分神预警 # 偏离驾驶位 # 打电话 # 抽烟 # 双手脱离方向盘 # 急加速 # 驾驶员未寄安全带
            return 3

        elif fatigue_type == 6:
            # 重度疲劳驾驶
            return 2

    if type == 22:
        # 驾驶超时
        return 18

    if type == 23 or type == 28:
        # 车内摄像头异常 # 完全遮挡摄像头
        return 11

    if type == 42 or type == 24 or type == 29:
        # 驾驶员变更  # 驾驶员不匹配 # 墨镜干扰识别
        return 32

    return 33
